// pages/mine/mineYouliao/mineYouliao.js
var app = getApp();
import Api from '../../../api/api.js';
const api = new Api();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    pageCount: 10,
    dataList: [],
    show:false,
    clickIndex:0,
  },
  moreClcik(e){
    var index = parseInt(e.currentTarget.dataset.key);
    this.data.clickIndex = index;
    this.setData({
      show: true
    });
  },
  floatClick(){
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  getListData() {
    var _this = this;
    var map = {};
    map.userId = app.USER_ID();
    map.pageCount = this.data.pageCount;
    map.pageIndex = this.data.pageIndex;
    api.getRequestData(app.miePostListUrl, map, true, "POST").then(res => {
      if (res.data.errorCode == 0) {
        var list = res.data.model.list;
        for (var i = 0; i < list.length; i++) {
          var dict = list[i];
          var imgs = JSON.parse(dict.imgs);
          dict.imgs = imgs;
          var content = dict.content;
          var contents = content.split("#");
          dict.contents = contents;

        }
        console.log(JSON.stringify(list[0]));
        if (list.length == 0) {
          app.alert("没有更多数据了～");
        }
        if (_this.data.pageIndex == 1) {
          _this.data.dataList = list;
          _this.setData({
            dataList: _this.data.dataList
          });
        } else {
          // 加载更多
          _this.setData({
            dataList: _this.data.dataList.concat(list)
          });
        }
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  itemClick(e){
    var id = e.currentTarget.dataset.key;
  },
  bottomItemClick(e){
    this.setData({
      show: false
    });
    var index = e.currentTarget.dataset.key;
    var dict = this.data.dataList[this.data.clickIndex];
    var ids = dict.id;
    if (index == 0) {
      this.goToEditPost();
    } else if (index == 1) {
         // 置顶
         this.zhiDingBeginClick(ids);
    } else if (index == 2) {
         // 删除
         this.beginDeleteClick(ids);
    } 
  },
  goToEditPost(){
    var dict = this.data.dataList[this.data.clickIndex];
    var userId = app.USER_ID();
    var id = dict.id;
    var path = '/pages/mine/editPost/editPost?USER_ID=' + userId + '&POST_ID=' + id;
    wx.navigateTo({
      url: path,
    })
  },
  zhiDingBeginClick(id){
    var _this = this;
    var map = {};
    map.id = id;
    api.getRequestData(app.postZhiDingUrl, map, false, "GET").then(res => {
      if (res.data.errorCode == 0) {
       app.alert("置顶成功~");
       setTimeout(function(){
        _this.data.pageIndex = 1;
        _this.getListData();
       },1400);
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  beginDeleteClick(id){
    this.deletePsot(id);
  },
  deletePsot(id){
    var _this = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个帖子?',
      confirmText:"确定删除",
      confirmColor:'#E02020',
      success: function (res) {
        if (res.confirm) {
          _this.beginDelete(id);
        } else if (res.cancel) {
        }
      }
    });
  },

  beginDelete (id) {
    var _this = this;
    var map = {};
    map.id = id;
    api.getRequestData(app.postDeleteUrl, map, false, "GET").then(res => {
      if (res.data.errorCode == 0) {
       app.alert("删除成功~");
       setTimeout(function(){
        _this.data.pageIndex = 1;
        _this.getListData();
       },1400);
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none',
          duration: 1500
        });
      }
    });
  },
  onPullDownRefresh: function() {
    this.data.pageIndex = 1;
    this.getListData();

  },
  updatePerViewimage(e){
    var ids = e.currentTarget.dataset.key+"";
    var idsArr = ids.split("_");
    var index = idsArr[0];
    var dict = this.data.dataList[index];
    var imgs = dict.imgs;
    index = idsArr[1];
    var url = imgs[index];
    wx.previewImage({
      current: url,
      urls: imgs,
    })
  },
  goToDetail(e){
    var index = e.currentTarget.dataset.key;
    var dict = this.data.dataList[index];
    var userId = app.USER_ID();
    var id = dict.id;
    var path = '/pages/index/detail/detail?USER_ID=' + userId + '&POST_ID=' + id;
    wx.navigateTo({
      url: path,
    })

  },
  goToVoteDetial(e){
    var id = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: '/pages/index/huaTiList/postList?topicId=' + id,
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex += 1;
    this.getListData();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})